package org.person.gift.controller;

import com.baomidou.mybatisplus.plugins.Page;
import org.person.gift.domain.ShopCar;
import org.person.gift.dto.AddShopCarDTO;
import org.person.gift.dto.DeleteDTO;
import org.person.gift.dto.UpdateNumDTO;
import org.person.gift.dto.UpdateSelectDTO;
import org.person.gift.query.ShopCarQuery;
import org.person.gift.service.IShopCarService;
import org.person.gift.util.AjaxResult;
import org.person.gift.util.PageList;
import org.person.gift.util.encrypt.TenantIdName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shopCar")
public class ShopCarController {
    @Autowired
    public IShopCarService shopCarService;


    /**
     * 保存和修改公用的
     * @param shopCar  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ShopCar shopCar){
        try {
            if( shopCar.getId()!=null)
                shopCarService.updateById(shopCar);
            else
                shopCarService.insert(shopCar);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopCarService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {

            ShopCar shopCar = shopCarService.selectById(id);
            return AjaxResult.me().setResultObj(shopCar);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< ShopCar> list = shopCarService.selectList(null);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody ShopCarQuery query)
    {
        Page<ShopCar> page = new Page<ShopCar>(query.getPage(),query.getRows());
        page = shopCarService.selectPage(page);
        PageList pageList = new PageList<ShopCar>(page.getTotal(),page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }
    /**
     * 新增购物车: userId skuId tenantId price num
     * 前端传参: 要不要传冗余参数??比如spuId spuName tenantName  price??
     * 新增购物车的时候,前端只需要传 skuId num就可以了  userId(在后端取当前登录人ID)
     * 不要让前端做过多的事情,一是因为前端的参数不是很可靠,后端能自己查就自己查
     *                     二是前端做的事情越多,后续联调就越麻烦
     * @return
     */
    @PostMapping("/add")
    public AjaxResult addShopCar(@RequestBody @Valid AddShopCarDTO dto){
        shopCarService.addShopCar(dto);
        return AjaxResult.me();
    }

    /**
     * 需要的参数: 需要修改的行,数量
     * 怎么定位到要修改的行??
     *   skuId + userId
     *   shopCarId
     * @return
     */
    @PostMapping("/updateNum")
    public AjaxResult updateNum(@RequestBody @Valid UpdateNumDTO dto){
        shopCarService.updateNum(dto);
        return AjaxResult.me();
    }

    @PostMapping("/updateSelect")
    public AjaxResult updateSelect(@RequestBody @Valid UpdateSelectDTO dto){
        shopCarService.updateSelect(dto);
        return AjaxResult.me();
    }

    @GetMapping("/list")
    public AjaxResult getShopCarList(Long userId){
        Map<TenantIdName,List<ShopCar>> map = shopCarService.getShopCarList(userId);
        return AjaxResult.me().setResultObj(map);
    }

    @DeleteMapping("/del")
    public AjaxResult delete(@RequestBody DeleteDTO dto){
        shopCarService.deleteByRedis(dto);
        return AjaxResult.me();
    }
}
